package com.demo.verification.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

/**
 * @author liangliang
 * 参考：https://juejin.im/post/5b6e7d0551882536e517b3e7?utm_source=gold_browser_extension
 */
public class VerificationUtil {

    private int width = 50; // 图片缓存区宽度
    private int height = 15; // 图片缓存区宽度
    private Random random = new Random(); // 随机数字
    private Color color = new Color(255,255,255); // 白色背景
    private String text; // 图片上的文字

    /**
     * 生成随机字符
     * @return
     */
    private char randomChar(){
        // 选取随机数字，生成字符
        String numbers = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
        int index = random.nextInt(numbers.length());
        // 转换为char类型
        return numbers.charAt(index);
    }

    /**
     * 生成随机颜色
     */
    private Color randomColor(){
        // 以RGB形式表示颜色
        int red = random.nextInt(255);
        int green = random.nextInt(255);
        int blue = random.nextInt(255);
        // 返回随机生成的颜色
        return new Color(red,green,blue);
    }

    /**
     * 干扰线
     * @param graphics
     */
    private void drawLine(Graphics graphics){

        for (int i = 0; i < 2; i++) {
            // 设置线的颜色
            graphics.setColor(randomColor());
            // 线的长度
            // 从（x1,y1）到（x2,y2）
            graphics.drawLine(random.nextInt(width),random.nextInt(height),random.nextInt(width),random.nextInt(height));
        }
    }

    /**
     * 创造图片缓存区
     * @return
     */
    private BufferedImage createBufferImage(){
        // 参数为宽度，高度，图像类型
        BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bufferedImage.getGraphics();
        // 设置背景色
        graphics.setColor(color);
        // 填充内容
        graphics.fillRect(0,0,width,height);
        return bufferedImage;
    }

    public BufferedImage getBufferImage(){
        // 创建缓存区
        BufferedImage bufferedImage = createBufferImage();
        // 得到绘制环境
        Graphics graphics = bufferedImage.getGraphics();
        // 验证码文本
        StringBuilder stringBuilder = new StringBuilder();

        // 长度为4
        for (int i = 0; i < 4; i++) {
            // 产生随机字符，char转换为String
            String str = String.valueOf(randomChar());
            stringBuilder.append(str);
            // 生成随机颜色
            graphics.setColor(randomColor());
            // 绘制文本
            graphics.drawString(str,i*width/4+(width/8),height);
        }
        // 添加干扰线
        drawLine(graphics);
        // 把生成的字符串付给文本，有助于验证
        text = stringBuilder.toString();
        return bufferedImage;
    }

    /**
     * 使用输出流打印图片
     * @param bufferedImage
     * @param outputStream
     */
    public static void output(BufferedImage bufferedImage, OutputStream outputStream) throws IOException {
        // 作为JPEG格式输出图片
        ImageIO.write(bufferedImage,"JPEG",outputStream);
    }


    public void setWidth(int width) {
        this.width = width;
    }


    public void setHeight(int height) {
        this.height = height;
    }

    public void setText(String text) {
        this.text = text;
    }
}
